package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.UserCollection;
import com.powernode.feign.UserCollectEsFeign;
import com.powernode.mapper.UserCollectionMapper;
import com.powernode.model.ProdEs;
import com.powernode.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
*内容: 
*作者: cy
*/
@Service
 public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService{

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Autowired
    private UserCollectEsFeign userCollectEsFeign;
    /**
     * 分页查询会员的收藏商品
     *
     * @param page
     * @param openId
     * @return
     */
    @Override
    public Page<ProdEs> selectUserCollectProdPage(Page<UserCollection> page, String openId) {
        Page<ProdEs> prodEsPage = new Page<>(page.getCurrent(), page.getSize());

        Page<UserCollection> userCollectionPage = userCollectionMapper.selectPage(page, new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, openId)
                .orderByDesc(UserCollection::getCreateTime)
        );
        List<UserCollection> collectionList = userCollectionPage.getRecords();
        if (CollectionUtils.isEmpty(collectionList)) {
            return prodEsPage;
        }
        // 拿到商品具体信息  prodIds
        List<Long> prodIds = collectionList.stream()
                .map(UserCollection::getProdId)
                .collect(Collectors.toList());
        // 远程调用 查询es
        List<ProdEs> prodEsList = userCollectEsFeign.getProdEsByProdId(prodIds);
        if (CollectionUtils.isEmpty(prodEsList)) {
            throw new RuntimeException("服务器维护中");
        }
        prodEsPage.setTotal(userCollectionPage.getTotal());
        prodEsPage.setRecords(prodEsList);
        return prodEsPage;
    }
    /**
     * 会员新增或者取消商品的收藏
     *
     * @param openId
     * @param prodId
     */
    @Override
    public void addOrCancel(String openId, Long prodId) {
        // 先确定唯一一条记录
        UserCollection userCollection = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, openId)
        );
        if (ObjectUtils.isEmpty(userCollection)) {
            UserCollection userCollection1 = new UserCollection();
            userCollection1.setCreateTime(new Date());
            userCollection1.setUserId(openId);
            userCollection1.setProdId(prodId);
            userCollectionMapper.insert(userCollection1);
            return;
        }
        // 删除
        userCollectionMapper.deleteById(userCollection.getId());
    }
}
